package com.extension.company.model.main;

import com.extension.company.Constant;
import com.extension.company.KEY;
import com.extension.company.MyApplication;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.common.utils.WXSingUtils;
import com.extension.company.contract.cart.FragmentCartContract;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.CarShopBean;
import com.extension.company.model.entity.PayForInfor;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：zwk
 * 时间：on 2019/3/27 15:55
 * 功能：购物车的Mode
 */
public class FragmentCartMode implements FragmentCartContract.Mode {
	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);
	private IWXAPI api;
	private String orderId;

	@Override
	public void carPayment(String goods_info) { // 购物车支付
		api = WXAPIFactory.createWXAPI(MyApplication.getContext(), KEY.WX_PAY_ID); // 微信支付初始化
		api.registerApp(KEY.WX_PAY_ID);
		retrofitUrlApi.carBuy(Constant.SIGN, Constant.TOKEN, goods_info)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<PayForInfor>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(PayForInfor payForInfor) {
						if (payForInfor.getCode() == 200) {
							String appid = payForInfor.getData().getAppid();
							String mchid = payForInfor.getData().getMch_id(); // 后台返回的商户号
							String nonceStr = payForInfor.getData().getNonce_str().get_$0(); // 后台的随机数
							String sign = payForInfor.getData().getSign().get_$0(); //后台返回的sign签名，需要本地处理
							String prePay = payForInfor.getData().getPrepay_id().get_$0();
							orderId = payForInfor.getData().getOut_trade_no(); //传送订单id
							// 回调接口
//							EventBus.getDefault().postSticky(new OrderId(orderId));
//							callback.orderIdCallback(orderId);

							PayReq req = new PayReq();
							req.appId = appid; // 你的微信的ID
							req.partnerId = mchid;  // 商户号
							req.prepayId = prePay;  // 预支付的交易会话id
							req.nonceStr = nonceStr;  // 随机生成的数字
							String time = String.valueOf(System.currentTimeMillis() / 1000); // 时间戳-秒
							req.timeStamp = time;  // 时间戳
							req.packageValue = "Sign=WXPay"; // 固定的填WXPay
							req.sign = WXSingUtils.signNum(mchid, prePay, "Sign=WXPay", nonceStr, time, KEY.WX_PAY_KEY);   // 签名
							api.sendReq(req);

						} else if (payForInfor.getCode() == 400) {
							ToastUtil.showToast(MyApplication.getContext(), payForInfor.getMsg());
						}
					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});

	}

	@Override
	public void carShopList(final FragmentCartContract.ShopListCallBack callBack) {  // 获取购物车的清单
		retrofitUrlApi.shopCarList(Constant.SIGN, "09a3fd5a-e614-45e8-b449-d268ab9f6179")
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<CarShopBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(CarShopBean carShopBean) {
						if (carShopBean.getCode() == 200 && carShopBean.getData().size() > 0) {
							callBack.shopCarM2P(carShopBean.getData());
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}
}
